package com.csun.nusing.ui.fragment;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.csun.nusing.R;
import com.csun.nusing.bean.UrlBean;
import com.csun.nusing.bean.jsonbean.FileSearchJsonBean;
import com.csun.nusing.bean.jsonbean.UserInfoJsonBean;
import com.csun.nusing.bean.jsonbean.UserInfoJsonBean.UserInfoDataBean;
import com.csun.nusing.presenter.FragmentNursingMinePresenter;
import com.csun.nusing.presenter.inter.IBase;
import com.csun.nusing.ui.activity.LoginActivity;
import com.csun.nusing.ui.activity.NursingAboutUsActivity;
import com.csun.nusing.ui.activity.NursingSettingActivity;
import com.csun.nusing.ui.activity.UserInfoActivity;
import com.csun.nusing.ui.familyactivity.FamilyMainActivity;
import com.csun.nusing.ui.myview.RoundImageView;
import com.csun.nusing.util.DialogUtils;
import com.csun.nusing.util.IntentUtils;
import com.csun.nusing.util.SPUtils;
import com.google.gson.Gson;
import com.home.administrator.okhttplibrary.IOkTaskCallback;
import com.home.administrator.okhttplibrary.OkHttpTask;

import java.util.HashMap;
import java.util.Map;

/**
 * 我的界面 ClassName: NursingMineFragment
 * 
 * @Description: TODO
 * @author fzq
 * @date 2017-10-27
 */
public class NursingMineFragment extends
		BaseFragment<FragmentNursingMinePresenter> implements IBase {

	public static final int UserInforequestCode = 41;
	private TextView userinfotv;
	private RoundImageView minetopriv;
	private TextView topnametv;
	private TextView aboutustv;
	private TextView settingtv;
	private String authorization;
	protected UserInfoJsonBean userInfoJsonBean;
	protected FileSearchJsonBean fillsearchbean;
	private int usersex;
	protected String userPic;
	private String username;
	private int userid;
	private int userPicId;
	private TextView changeroletv;
	public Intent intent;
	private TextView outtv;

	public NursingMineFragment() {
	}

	public static NursingMineFragment newInstance() {
		return new NursingMineFragment();
	}

	@Override
	public int getContentViewId() {
		return R.layout.fragment_nursing_mine;
	}

	// findView
	@Override
	public void initSubViews(View view) {
		super.initSubViews(view);
		findView(view);
		controlView();
	}

	/**
	 * 控件
	 * 
	 * @Description: TODO
	 * @param @param view
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-10-20
	 */
	private void findView(View view) {
		minetopriv = view
				.findViewById(R.id.nursing_mine_top_riv);
		userinfotv = view
				.findViewById(R.id.fragment_nursing_mine_user_info_tv);
		topnametv = view.findViewById(R.id.nursing_mine_top_name_tv);

		settingtv = view
				.findViewById(R.id.fragment_nursing_mine_user_setting_tv);
		outtv = view.findViewById(R.id.fragment_nursing_mine_out_tv);
		aboutustv = view
				.findViewById(R.id.fragment_nursing_mine_user_us_tv);
		changeroletv = view
				.findViewById(R.id.fragment_family_mine_bottom_changerole_tv);
	}

	/**
	 * 控制点击事件
	 * 
	 * @Description: TODO
	 * @param
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-10-27
	 */
	private void controlView() {
		userinfotv.setOnClickListener(new click());
		settingtv.setOnClickListener(new click());
		aboutustv.setOnClickListener(new click());
		changeroletv.setOnClickListener(new click());
		outtv.setOnClickListener(new click());
	}

	// 交给presenter处理数据逻辑
	@Override
	public void initData() {
		super.initData();
		authorization = (String) SPUtils
				.get(getActivity(), "authorization", "");
		OkHttpTask.newInstance(getActivity())
				.addHeader("authorization", authorization)
				.enqueue(new IOkTaskCallback() {
					@Override
					public void onSuccess(String result) {
						if (result != null) {
							Log.e("fzq", "---" + result);
							Gson gson = new Gson();
							userInfoJsonBean = gson.fromJson(result,
									UserInfoJsonBean.class);
							if (userInfoJsonBean != null
									&& userInfoJsonBean.getData() != null) {
								controlData(userInfoJsonBean.getData());
							}
						}
					}
				}).start(UrlBean.USERINFOURL);
	}

	/**
	 * 将数据保存
	 * 
	 * @Description: TODO
	 * @param @param data
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-11-2
	 */
	protected void controlData(UserInfoDataBean data) {
		username = data.getRealname();
		userPicId = data.getHeadpicid();
		if (username != null && !username.equals("null")) {
			topnametv.setText("" + data.getRealname());
		}else{
			topnametv.setText(getString(R.string.please_write_name));
		}
		/**
		 * 请求头部图片
		 */
		Map<String, String> param = new HashMap<String, String>();
		param.put("resourceid", "" + data.getHeadpicid());
		OkHttpTask
				.newInstance(getActivity())
				.addHeader("authorization", authorization)
				.enqueue(new IOkTaskCallback() {
					@Override
					public void onSuccess(String result) {
						Gson gson = new Gson();
						fillsearchbean = gson.fromJson(result,
								FileSearchJsonBean.class);
						if (fillsearchbean != null) {
							userPic = fillsearchbean.getData()
									.getResourcepath();
							Glide.with(getActivity())
									.load(fillsearchbean.getData()
											.getResourcepath())
									.into(minetopriv);
						}
					}
				})
				.start(UrlBean.FILESEARCHURL + "?resourceid="
						+ data.getHeadpicid());
		usersex = data.getSex();
		userid = data.getUserid();
		for (int i = 0; i < data.getRoles().size(); i++) {
			if (data.getRoles().get(i).getRoleid() == 3) {
				changeroletv.setVisibility(View.VISIBLE);
			}
		}
	}

	@Override
	protected FragmentNursingMinePresenter getPresenter() {
		return new FragmentNursingMinePresenter(this);
	}

	/**
	 * 点击事件 ClassName: click
	 * 
	 * @Description: TODO
	 * @author fzq
	 * @date 2017-11-2
	 */
	private class click implements OnClickListener {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.fragment_nursing_mine_user_info_tv:
				intent = new Intent(getActivity(), UserInfoActivity.class);
				intent.putExtra("username", "" + username);
				intent.putExtra("usersex", "" + usersex);
				intent.putExtra("userPic", "" + userPic);
				intent.putExtra("userPicId", "" + userPicId);
				intent.putExtra("userid", "" + userid);
				startActivityForResult(intent, UserInforequestCode);
				break;
			case R.id.fragment_nursing_mine_user_setting_tv:
				// 设置
				IntentUtils.startActivity(getActivity(),
						NursingSettingActivity.class);
				break;
			case R.id.fragment_nursing_mine_user_us_tv:
				// 关于我们
				IntentUtils.startActivity(getActivity(),
						NursingAboutUsActivity.class);
				break;
			case R.id.fragment_family_mine_bottom_changerole_tv:
				intent = new Intent(getActivity(), FamilyMainActivity.class);
				SPUtils.put(getActivity(), "isNursingFlag", false);
				startActivity(intent);
				getActivity().finish();
				getActivity().overridePendingTransition(R.anim.fade_in,
						R.anim.fade_out);
				break;
			case R.id.fragment_nursing_mine_out_tv:
				//注销功能实现
				DialogUtils.showTitleMessageDialog(getActivity(),getString(R.string.ti_si),getString(R.string.is_out_login), new DialogUtils.OnButtonClickListener() {
					@Override
					public void onConfirmButtonClick() {
						//确定按钮的操作
						SPUtils.put(getActivity(), "USERNAME", "");
						SPUtils.put(getActivity(), "PASSWORD", "");
						getActivity().startActivity(new Intent(getActivity(), LoginActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
						getActivity().finish();
					}
					@Override
					public void onCancelButtonClick() {
						super.onCancelButtonClick();
					}
				});
				break;
			default:
				break;
			}
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Log.e("fzq", "---------------onActivityResult");
		if (requestCode == UserInforequestCode) {
			// 刷新头像
			if (resultCode == 0x55) {
				if (userPic != data.getStringExtra("userPicPath")) {
					userPic = data.getStringExtra("userPicPath");
					Glide.with(getActivity()).load(userPic).into(minetopriv);
				}
				username = data.getStringExtra("username");
				topnametv.setText("" + username);
				if (data.getStringExtra("usersex").equals("0")) {
					usersex = 0;
				} else {
					usersex = 1;
				}
			}
		}
	}

}
